<!DOCTYPE html>
<html>
  <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta content="yes" name="apple-mobile-web-app-capable" />
  <meta content="black" name="apple-mobile-web-app-status-bar-style" />
  <meta name="referrer" content="never">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <meta name="author" content="kveln">
  <title> 基于kubekey部署kubernetes生产级集群 | John Wong&#39;s Blog</title>
  <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
  <!-- <link href="https://blog.jwangkun.com/media/css/bootstrap.min.css" rel="stylesheet"> -->
  <!--  <link href="https://blog.jwangkun.com/media/css/all.min.css" rel="stylesheet" type="text/css"> -->
  <link href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
  <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
  <link rel="alternate" type="application/rss+xml" title=" 基于kubekey部署kubernetes生产级集群 | John Wong&#39;s Blog » Feed" href="https://blog.jwangkun.com/atom.xml">
  <link rel="stylesheet"href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/styles/androidstudio.min.css">
  <link href="https://blog.jwangkun.com/styles/main.css" rel="stylesheet">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.10/build/highlight.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/jquery.min.js"></script> -->
  <script>hljs.initHighlightingOnLoad();</script>
  

    <meta property="og:description" content=" 基于kubekey部署kubernetes生产级集群"/>
    <meta property="og:url" content="https://blog.jwangkun.com/post/cIJwT12am/"/>
    <meta property="og:locale" content="zh-CN"/>
    <meta property="og:type" content="website"/>
    <meta property="og:site_name" content="John Wong&#39;s Blog"/>
  </head>
  <body>
  	<!-- Navigation -->
  <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
    <div class="container">
      <a class="navbar-brand" href="https://blog.jwangkun.com">John Wong&#39;s Blog</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
        Menu
        <i class="fas fa-bars"></i>
      </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          
          <li class="nav-item">
              
              <a class="nav-link" href="/">首页</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/archives">归档</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/tags">标签</a>
              
          </li>
          
          <li class="nav-item">
              
              <a class="nav-link" href="/post/about">关于</a>
              
          </li>
          
        </ul>
      </div>
    </div>
  </nav>
  <!-- Page Header -->
  <header class="masthead" style="background-image: url('https://blog.jwangkun.com/media/images/home-bg.jpg')">
    <div class="overlay"></div>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="post-heading">
          	<span class="tags">
          	 
        </span>
            <h1> 基于kubekey部署kubernetes生产级集群</h1>
            <span class="meta">
            	Posted on
              2020-10-12，9 min read
            </span>
          </div>
        </div>
      </div>
    </div>
  </header>

  <!-- Post Content -->
  <article>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <p>kubeykey是KubeSphere基于Go 语言开发的kubernetes集群部署程序。使用 KubeKey，您可以轻松、高效、灵活地单独或整体安装 Kubernetes 和 KubeSphere。</p>
<!-- more -->
<p>有三种情况可以使用 KubeKey。</p>
<ul>
<li>仅安装 Kubernetes</li>
<li>使用一个命令安装 Kubernetes 和 KubeSphere</li>
<li>首先安装 Kubernetes，然后使用ks-installer 在其上部署 KubeSphere</li>
</ul>
<p>重要提示：Kubekey 将会帮您安装 Kubernetes，若已有 Kubernetes 集群请参考 <a href="https://github.com/kubesphere/ks-installer/">在 Kubernetes 之上安装 KubeSphere</a>。</p>
<p>优势</p>
<ul>
<li>基于 Ansible 的安装程序具有大量软件依赖性，例如 Python。</li>
<li>KubeKey 是使用 Go<br>
语言开发的，可以消除在各种环境中出现的问题，从而提高安装成功率。<br>
KubeKey 使用 Kubeadm 在节点上尽可能多地并行安装 K8s<br>
集群，以降低安装复杂性并提高效率。与较早的安装程序相比，它将大大节省安装时间。<br>
KubeKey 支持将群集从 all-in-one<br>
扩展到多节点群集甚至 HA 集群。 KubeKey 旨在将群集当作一个对象操作，即 CaaO。</li>
</ul>
<p>参考：https://github.com/kubesphere/kubekey</p>
<h2 id="kubekey部署kubernetes集群">kubekey部署kubernetes集群</h2>
<p>下载kk部署工具</p>
<pre><code class="language-shell">wget https://github.com/kubesphere/kubekey/releases/download/v1.0.0/kubekey-v1.0.0-linux-amd64.tar.gz
tar -zxvf kubekey-v1.0.0-linux-amd64.tar.gz
mv kk /usr/local/bin/
</code></pre>
<h2 id="部署all-in-one单节点">部署all-in-one单节点</h2>
<p>安装依赖（可选）</p>
<pre><code class="language-shell">yum install -y socat conntrack
</code></pre>
<p>仅部署kubernetes单节点</p>
<pre><code class="language-shell">kk create cluster
</code></pre>
<p>同时部署kubernetes和kubesphere，可指定kubernetes版本或kubesphere版本</p>
<pre><code class="language-shell">kk create cluster --with-kubernetes v1.18.6 --with-kubesphere v3.0.0
</code></pre>
<h2 id="部署多节点集群">部署多节点集群</h2>
<p>环境准备，确认节点时间同步即可，无需配置主机名，kubekey会自动纠正主机名。</p>
<pre><code class="language-shell">yum install -y chrony
systemctl enable --now chronyd
timedatectl set-timezone Asia/Shanghai
</code></pre>
<p>创建示例配置文件</p>
<pre><code class="language-shell">kk create config
</code></pre>
<p>创建示例配置文件，同时部署kubernetes和kubesphere，可指定版本、配置文件名称及保存路径</p>
<pre><code class="language-shell">kk create config --with-kubernetes v1.18.6 --with-kubesphere v3.0.0 -f /root/config-sample.yaml
</code></pre>
<p>根据您的环境修改配置文件 config-sample.yaml，以下示例以部署3个master节点和1个node节点为例(不执行kubesphere部署，仅搭建kubernetes集群)：</p>
<pre><code class="language-yaml"># vim config-sample.yaml
apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  - {name: node1, address: 192.168.1.110, internalAddress: 192.168.1.110, user: root, password: 123456}
  - {name: node2, address: 192.168.1.111, internalAddress: 192.168.1.111, user: root, password: 123456}
  - {name: node3, address: 192.168.1.112, internalAddress: 192.168.1.112, user: root, password: 123456}
  - {name: node4, address: 192.168.1.113, internalAddress: 192.168.1.113, user: root, password: 123456}
  roleGroups:
    etcd:
    - node1
    - node2
    - node3
    master: 
    - node1
    - node2
    - node3
    worker:
    - node4
  controlPlaneEndpoint:
    domain: lb.kubesphere.local
    address: &quot;&quot;
    port: &quot;6443&quot;
  kubernetes:
    version: v1.17.9
    imageRepo: kubesphere
    clusterName: cluster.local
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
  registry:
    registryMirrors: []
    insecureRegistries: []
  addons: []
</code></pre>
<p>当指定安装KubeSphere时，要求集群中有可用的持久化存储。默认使用localVolume，如果需要使用其他持久化存储，请参阅<a href="https://github.com/kubesphere/kubekey/blob/master/docs/addons.md">addons</a>配置。</p>
<p>使用配置文件创建集群。</p>
<pre><code class="language-shell">kk create cluster -f config-sample.yam
</code></pre>
<p>完成后查看集群状态</p>
<pre><code class="language-shell">[root@node1 ~]# kubectl get nodes -o wide
NAME    STATUS   ROLES    AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
node1   Ready    master   3m55s   v1.17.9   192.168.1.110   &lt;none&gt;        CentOS Linux 7 (Core)   3.10.0-1127.18.2.el7.x86_64   docker://19.3.8
node2   Ready    master   3m20s   v1.17.9   192.168.1.111   &lt;none&gt;        CentOS Linux 7 (Core)   3.10.0-1127.18.2.el7.x86_64   docker://19.3.8
node3   Ready    master   3m13s   v1.17.9   192.168.1.112   &lt;none&gt;        CentOS Linux 7 (Core)   3.10.0-1127.18.2.el7.x86_64   docker://19.3.8
node4   Ready    worker   3m23s   v1.17.9   192.168.1.113   &lt;none&gt;        CentOS Linux 7 (Core)   3.10.0-1127.18.2.el7.x86_64   docker://19.3.8
[root@node1 ~]# 
[root@node1 ~]# 
[root@node1 ~]# kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-59d85c5c84-mzfz9   1/1     Running   0          3m31s
kube-system   calico-node-j5gx7                          1/1     Running   0          3m26s
kube-system   calico-node-n4vwj                          1/1     Running   0          3m31s
kube-system   calico-node-tdgrv                          1/1     Running   0          3m24s
kube-system   calico-node-zc27d                          1/1     Running   0          3m17s
kube-system   coredns-74d59cc5c6-kspd4                   1/1     Running   0          3m41s
kube-system   coredns-74d59cc5c6-ll7kt                   1/1     Running   0          3m41s
kube-system   kube-apiserver-node1                       1/1     Running   0          3m53s
kube-system   kube-apiserver-node2                       1/1     Running   0          3m22s
kube-system   kube-apiserver-node3                       1/1     Running   0          2m12s
kube-system   kube-controller-manager-node1              1/1     Running   0          3m53s
kube-system   kube-controller-manager-node2              1/1     Running   0          3m21s
kube-system   kube-controller-manager-node3              1/1     Running   0          115s
kube-system   kube-proxy-hk6c5                           1/1     Running   0          3m41s
kube-system   kube-proxy-k5d8w                           1/1     Running   0          3m17s
kube-system   kube-proxy-lv6wk                           1/1     Running   0          3m27s
kube-system   kube-proxy-pqdgb                           1/1     Running   0          3m24s
kube-system   kube-scheduler-node1                       1/1     Running   0          3m53s
kube-system   kube-scheduler-node2                       1/1     Running   0          3m21s
kube-system   kube-scheduler-node3                       1/1     Running   0          116s
kube-system   nodelocaldns-6kq4c                         1/1     Running   0          3m27s
kube-system   nodelocaldns-7xbc9                         1/1     Running   0          3m24s
kube-system   nodelocaldns-9r7v4                         1/1     Running   0          3m41s
kube-system   nodelocaldns-rkv2d                         1/1     Running   0          3m17s

</code></pre>
<h2 id="启用多集群管理">启用多集群管理</h2>
<p>默认情况下，Kubekey 将仅安装一个 Solo 模式的单集群，即未开启 Kubernetes 多集群联邦。如果您希望将 KubeSphere 作为一个支持多集群集中管理的中央面板，您需要在<a href="https://github.com/kubesphere/kubekey/blob/master/docs/config-example.md">config-example.yaml</a>中设置<code>ClusterRole</code>。关于多集群的使用文档，请参考<a href="https://github.com/kubesphere/community/blob/master/sig-multicluster/how-to-setup-multicluster-on-kubesphere/README_zh.md">如何启用多集群</a>。</p>
<h3 id="开启可插拔功能组件">开启可插拔功能组件</h3>
<p>KubeSphere 从 2.1.0 版本开始对 Installer 的各功能组件进行了解耦，快速安装将默认仅开启最小化安装（Minimal Installation），Installer 支持在安装前或安装后自定义可插拔的功能组件的安装。使最小化安装更快速轻量且资源占用更少，也方便不同用户按需选择安装不同的功能组件。</p>
<p>KubeSphere 有多个可插拔功能组件，功能组件的介绍可参考<a href="https://github.com/kubesphere/kubekey/blob/master/docs/config-example.md">配置示例</a>。您可以根据需求，选择开启安装 KubeSphere 的可插拔功能组件。我们非常建议您开启这些功能组件来体验 KubeSphere 完整的功能以及端到端的解决方案。请在安装前确保您的机器有足够的 CPU 与内存资源。开启可插拔功能组件可参考<a href="https://github.com/kubesphere/ks-installer/blob/master/README_zh.md#%E5%AE%89%E8%A3%85%E5%8A%9F%E8%83%BD%E7%BB%84%E4%BB%B6">开启可选功能组件</a>。</p>
<h3 id="添加节点">添加节点</h3>
<p>将新节点的信息添加到群集配置文件，然后应用更改。</p>
<pre><code class="language-shell">kk add nodes -f config-sample.yaml
</code></pre>
<h3 id="删除节点">删除节点</h3>
<p>通过以下命令删除节点，nodename指需要删除的节点名。</p>
<pre><code class="language-shell">kk delete node &lt;nodeName&gt; -f config-sample.yaml
</code></pre>
<h3 id="删除集群">删除集群</h3>
<p>如果您以快速入门（all-in-one）开始：</p>
<pre><code class="language-shell">kk delete cluster
</code></pre>
<p>如果从高级安装开始（使用配置文件创建的集群）：</p>
<pre><code class="language-shell">kk delete cluster [-f config-sample.yaml]
</code></pre>
<h3 id="集群升级">集群升级</h3>
<p>单节点集群，升级单集群到指定版本。</p>
<pre><code class="language-shell">kk upgrade [--with-kubernetes version] [--with-kubesphere version]
</code></pre>
<ul>
<li><code>--with-kubernetes</code>指定kubernetes目标版本。</li>
<li><code>--with-kubesphere</code>指定kubesphere目标版本。</li>
</ul>
<p>多节点集群，通过指定配置文件对集群进行升级。</p>
<pre><code class="language-shell">kk upgrade [--with-kubernetes version] [--with-kubesphere version] [(-f | --file) path]
</code></pre>
<ul>
<li><code>--with-kubernetes</code>指定kubernetes目标版本。</li>
<li><code>--with-kubesphere</code>指定kubesphere目标版本。</li>
<li><code>-f</code>指定集群安装时创建的配置文件。</li>
</ul>
<p>注意: 升级多节点集群需要指定配置文件. 如果集群非kubekey创建，或者创建集群时生成的配置文件丢失，需要重新生成配置文件，或使用以下方法生成。</p>
<p>Getting cluster info and generating kubekey’s configuration file (optional).</p>
<pre><code class="language-shell">kk create config [--from-cluster] [(-f | --file) path] [--kubeconfig path]
</code></pre>
<ul>
<li><code>--from-cluster</code>根据已存在集群信息生成配置文件.</li>
<li><code>-f</code>指定生成配置文件路径.</li>
<li><code>--kubeconfig</code>指定集群kubeconfig文件.</li>
<li>由于无法全面获取集群配置，生成配置文件后，请根据集群实际信息补全配置文件</li>
</ul>

          
          <p class="next-post">下一篇：
            <a href="https://blog.jwangkun.com/post/-6mGRsKnd/">
              <span class="post-title">
                 使用easzup 快速部署一个 kubernetes的高可用集群&rarr;
              </span>
            </a>
          </p>
        
        <div class="comment">
          
        </div>
      </div>
    </div>
  </article>
 <!-- Footer -->
  <footer>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <ul class="list-inline text-center">
            
            
              
            
              
            
              
            
              
            
              
            
              
            
              
              <li class="list-inline-item">
              <a href="https://blog.jwangkun.com/atom.xml" target="_blank">
                <span class="fa-stack fa-lg">
                  <i class="fas fa-circle fa-stack-2x"></i>
                  <i class="fas fa-rss fa-stack-1x fa-inverse"></i>
                </span>
              </a>
              </li>
          </ul>
          <p class="copyright text-muted">Copyright &copy;<span>John Wong&#39;s Blog</span><br><a href="https://github.com/getgridea/gridea" class="Themeinfo">Powered by Gridea</a></p>
        </div>
      </div>
    </div>
   </footer>
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/bootstrap.bundle.min.js"></script> -->
  <!-- Bootstrap core JavaScript -->
  <script src="https://cdn.jsdelivr.net/gh/Alanrk/clean-cdn@1.0/scripts/clean-blog.min.js"></script>
  <!-- <script src="https://blog.jwangkun.com/media/scripts/clean-blog.min.js"></script> -->
  <script src="//instant.page/3.0.0" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>
  <style type="text/css">a.back_to_top{text-decoration:none;position:fixed;bottom:40px;right:30px;background:#f0f0f0;height:40px;width:40px;border-radius:50%;line-height:36px;font-size:18px;text-align:center;transition-duration:.5s;transition-propety:background-color;display:none}a.back_to_top span{color:#888}a.back_to_top:hover{cursor:pointer;background:#dfdfdf}a.back_to_top:hover span{color:#555}@media print,screen and(max-width:580px){.back_to_top{display:none!important}}</style>
<a id="back_to_top" href="#" class="back_to_top">
  <span>▲</span></a>
<script>$(document).ready((function(_this) {
    return function() {
      var bt;
      bt = $('#back_to_top');
      if ($(document).width() > 480) {
        $(window).scroll(function() {
          var st;
          st = $(window).scrollTop();
          if (st > 30) {
            return bt.css('display', 'block')
          } else {
            return bt.css('display', 'none')
          }
        });
        return bt.click(function() {
          $('body,html').animate({
            scrollTop: 0
          },
          800);
          return false
        })
      }
    }
  })(this));
  var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?84ab85460bfbe79dbe5776a1df139a8f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
  </script>
  
<script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1279350888&web_id=1279350888"></script>

  </body>
</html>

